/**
 * Created with IntelliJ IDEA.
 * Description:判断是否为平衡二叉树
 * User: wangxin
 * Date: 2024-11-09
 * Time: 22:06
 */
public class test2 {
    public int func(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftL = func(root.left);
        int rightL = func(root.right);
        return (leftL > rightL) ? leftL + 1 : rightL + 1;
    }
    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if(pRoot == null){
            return true;
        }
        int c =  func(pRoot.left);
        int d =  func(pRoot.right);
        if((c-d) > 1||(c-d) < -1){
            return false;
        }
        return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
    }
}
